package UserManager;
import Method.Conexcute;
import java.io.IOException;
import java.io.File;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(urlPatterns = "/Adduser")
@MultipartConfig(location = "D:\\picture")// location改为本地的绝对路径
public class AdduserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
       String name= request.getParameter("name");
       String password=request.getParameter("password");
       String sex=request.getParameter("sex");
        Part part = request.getPart("file");
        String fileName = part.getSubmittedFileName();
        part.write(fileName);

       System.out.println(fileName);
       String image=fileName;
        Conexcute conexcute=new Conexcute();
        System.out.println(sex);
        System.out.println("insert into `user` (`us_name`,`password`,`sex`,`us_image`) values ( '"+name+"', '"+password+"','"+sex+"','"+image+"');");
        if(conexcute.execute("insert into `user` (`us_name`,`password`,`sex`,`us_image`) values ( '"+name+"', '"+password+"','"+sex+"','"+image+"');")){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().println("数据添加成功");
            response.sendRedirect("UserManager/user.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
